import number from "@/mount/global/number"
import structure from "@/mount/structure"
import { isInArray } from "@/utils"
import { MissonModel } from "../Misson/missonModel"

/* 定义其他相关任务 */
export default class RoomTaskOther extends Room {
    /* 建筑任务监控 适用爬虫 vindicat01 */
    public BCT_PAS():void{
        /* 该任务不是常需要的，所以相对惰性一点 */
        if (Game.time % 41) return
        var construction_ = this.find(FIND_CONSTRUCTION_SITES)
        if (construction_.length > 0)
            global.DicCreepFrameControl[this.name]['vindicat01'].num = 1
    }
    /* 实验室清理任务监控 */
    public CTL_PAS():void{
        if (Game.time % 32 ) return
        if (!this.memory.TaskMemory.labs) return
        var occupiedLab = Object.keys(this.memory.TaskData.OccupiedLab)
        var labs = this.memory.TaskMemory.labs as string[]
        for (var i of labs)
        {
            if (!isInArray(occupiedLab,i))
            {
                /* 说明未被占用，检查是否有除了能量外的其他残余物，有的话就清除 */
                var thisLab = Game.getObjectById(i) as StructureLab
                if (!thisLab) {
                    delete this.memory.TaskData.OccupiedLab[i]
                    var index = labs.indexOf(i)
                    labs.splice(index,1)
                    continue}
                for (var r in thisLab.store)
                {
                    if (r != 'energy' && !this.getMisson_S('CREEP','物流搬运','transport01'))
                    {
                        /* 下达搬运任务 */
                        var TaskCreep:RoomTask = {} as RoomTask
                        Object.assign(TaskCreep,MissonModel['物流搬运'])
                        TaskCreep.relateCreep = ['transport01']
                        TaskCreep.sourcenum = 0
                        TaskCreep.relateResource = [r as ResourceConstant]
                        TaskCreep.disStructure = [this.memory.TaskMemory.storageID as string]
                        TaskCreep.taskLevel = 1
                        TaskCreep.sourceStructure = [thisLab.id]
                        this.mountMisson(TaskCreep)
                        return
                    }
                }
            }
        }
        
    }
    /* 房间签名任务主动处理 */
    public SG_AC():void{
        if (Game.time % 27) return
        var num = this.CheckMissonNum('CREEP','房间签名')
        if (num > 0)
        {
            global.DicCreepFrameControl[this.name]['scout'].num = num
        }
    }
    /* 原矿开采任务主动处理 */
    public RMH_AC():void{
        if (Game.time % 15) return
        if (!this.CheckInMisson('CREEP','原矿开采')) return
        var thisTask = this.getMisson('CREEP','原矿开采')
        var mineral_ = Game.getObjectById(this.memory.TaskMemory.mineralID as string) as Mineral
        if (thisTask.state == 0)
        {
            /* 准备阶段 判断是否有extractor */
            if (!this.memory.TaskMemory.extractID) return
            if (mineral_.pos.findInRange(FIND_STRUCTURES,1,{filter:(structure)=>{
                return structure.structureType == 'container'
            }}).length <= 0) return
            thisTask.state = 1
        }
        else if (thisTask.state == 1)
        {
            /* 工作阶段 */
            if (mineral_.ticksToRegeneration) thisTask.state = 2
            else global.DicCreepFrameControl[this.name]['miner'].num = 1

            if (mineral_.pos.findInRange(FIND_STRUCTURES,1,{filter:(structure)=>{
                return structure.structureType == 'container'
            }}).length <= 0) thisTask.state = 0
        }
        else if (thisTask.state == 2)
        {
            if (!mineral_.ticksToRegeneration)
                thisTask.state = 1
            var storage_ = Game.getObjectById(this.memory.TaskMemory.storageID as string) as StructureStorage
            if (!storage_ || storage_.store[thisTask.relateResource[0]] > this.memory.TaskData.mineralNum?this.memory.TaskData.mineralNum:300000)
                this.deleteMisson('CREEP','原矿开采')
        }
    }
    /* 外矿开采任务主动处理 */
    public OMH_AC():void{
        if (Game.time % 19) return
        var missionNum = this.CheckMissonNum('CREEP','外矿开采')
        if (missionNum <= 0) return
        var NumOfDefend = 0
        var NumOfClaim = 0
        var NumOfCarry = 0
        var NumOfMine = 0
        LoopOut:
        for (var misson of this.memory.Task['CREEP'])
        {
        if (misson.Name == '外矿开采')
        {
            /* 执行逻辑 */
            misson.numData['out-claim'] = 1
            var disRoom = Game.rooms[misson.disRoom[0]]
            if (disRoom)
            {
                /* 载入OutMineData */
                if (!misson.OutMineData)
                {
                    misson.OutMineData = {}
                    var conrtol_ = disRoom.controller
                    var conrtol_pos = `${conrtol_.pos.x}/${conrtol_.pos.y}/${disRoom.name}`
                    misson.OutMineData['control'] = conrtol_pos
                    var sources_ = disRoom.find(FIND_SOURCES)
                    if (sources_.length > 0)
                    {
                        var source_pos = []
                    for (var s of sources_)
                    {
                        source_pos.push(`${s.pos.x}/${s.pos.y}/${disRoom.name}`)
                    }
                        misson.OutMineData['source'] = source_pos
                    }
                    else {delete misson.OutMineData;return}
                    misson.OutMineData['sourceNum'] = misson.OutMineData['source'].length
                }
                else
                {
                    /* 监测container的动态 */
                    var containers = disRoom.getStructure(STRUCTURE_CONTAINER) as StructureContainer[]
                    if (!misson.OutMineData['container']) misson.OutMineData['container'] = []
                    if (containers.length > 0 )
                    {
                        for (var i of containers)
                            if (!isInArray(misson.OutMineData['container'],`${i.pos.x}/${i.pos.y}/${disRoom.name}`))
                                misson.OutMineData['container'].push(`${i.pos.x}/${i.pos.y}/${disRoom.name}`)
                    }
                }
            }
            if (misson.state == 0)
            {
                /* 没有造好container的情况 */
                misson.bindData['out-min'].num = misson.num
                misson.bindData['out-car'].num = 0
                misson.bindData['out-defend'].num = 0
                if (disRoom && disRoom.getStructure(STRUCTURE_CONTAINER).length >= misson.num)
                {
                    misson.state = 1
                }
            }
            else if (misson.state == 1)
            {
                /* 正常的开采状态 */
                if (disRoom && disRoom.getStructure(STRUCTURE_CONTAINER).length < misson.num)
                {
                    misson.state = 0
                    continue LoopOut
                }
                misson.bindData['out-min'].num = misson.num
                misson.bindData['out-car'].num = misson.num
                misson.bindData['out-defend'].num = 0
            }
            else if (misson.state == 2)
            {
                /* 遭遇敌军的状态 */
                if (disRoom)
                {
                    if (
                        disRoom.find(FIND_HOSTILE_CREEPS,{filter:(creep)=>{
                        return !isInArray(Memory.whitesheets,creep.owner.username)
                    }}).length <= 0 && disRoom.find(FIND_STRUCTURES,{filter:(structure)=>{
                        return structure.structureType == STRUCTURE_INVADER_CORE
                    }}).length <= 0
                    )
                    {misson.state = 1;continue LoopOut}
                }
                misson.bindData['out-min'].num = 0
                misson.bindData['out-car'].num = 0
                misson.bindData['out-defend'].num = 2
            }
            NumOfDefend += misson.bindData['out-defend'].num
            NumOfClaim += misson.bindData['out-claim'].num
            NumOfCarry += misson.bindData['out-car'].num
            NumOfMine += misson.bindData['out-min'].num
        }
        }
        global.DicCreepFrameControl[this.name]['out-claim'].num = NumOfClaim
        global.DicCreepFrameControl[this.name]['out-car'].num = NumOfCarry
        global.DicCreepFrameControl[this.name]['out-min'].num = NumOfMine
        global.DicCreepFrameControl[this.name]['out-defend'].num = NumOfDefend
    }
    /* 核弹填充任务主动处理 */
    public FN_AC():void{
        var thisTask = this.getMisson('ROOM',"核弹填充")
        if (!thisTask) return
        var storage_ = Game.getObjectById(this.memory.TaskMemory.storageID as string) as StructureStorage
        var nuke_ = Game.getObjectById(this.memory.TaskMemory.NukerID as string) as StructureNuker
        var terminal_ = Game.getObjectById(this.memory.TaskMemory.terminalID as string) as StructureTerminal
        if (!storage_ || !nuke_ || !terminal_)
        {
            this.deleteMisson_ID(thisTask.ID)
            return
        }
        if (thisTask.state == 0)
        {
            // console.log(nuke_.store.getUsedCapacity('G'))
            /* 填充G */
            if (nuke_.store.getUsedCapacity('G')>= 5000)
            {
                thisTask.state = 1
                return
            }
            else
            {
                /* 首先确认storage中是否有足够的G，不够的话自己买 */
                if (storage_.store.getUsedCapacity('G')+nuke_.store.getUsedCapacity('G') < 5000)
                {
                    // if (terminal_.store.getUsedCapacity('G')> 0)
                    // {
                    //     if (!thisTask['G']) thisTask['G'] = 0
                    //     thisTask.Data['G'] = this.memory.TaskData.TerminalResource['G']
                    //     this.memory.TaskData.TerminalResource['G'] = 0
                    //     return
                    // }
                    /* 寻找市场上是否有G的订单,有的话并且cr足够就购买6000 */
                    var cr = Game.market.credits
                    var marketGlist = Game.market.getAllOrders({type: ORDER_SELL, resourceType: 'G'})
                    let disOrder:Order = marketGlist[0]
                    for (var i of marketGlist)
                    {
                        if (i.price < disOrder.price && i.amount >= 5000 && terminal_.store['energy'] > 5000)
                        {
                            disOrder = i
                        }
                    }
                    if (!disOrder){return}
                    if (Game.market.deal(disOrder.id,6000,this.name) == OK)
                        return
                    
                }
                if (this.CheckInMisson('CREEP','物流搬运')) return
                let Task1:RoomTask = {} as RoomTask
                Object.assign(Task1,MissonModel['物流搬运'])
                Task1.relateCreep = ['transport01']
                Task1.disnum = 5000
                Task1.relateResource = ['G']
                Task1.disStructure = [nuke_.id]
                Task1.sourceStructure = [this.memory.TaskMemory.storageID as string]
                this.mountMisson(Task1)
            }
        }
        else if (thisTask.state == 1)
        {
            /* 填充energy */
            // console.log(nuke_.store.getUsedCapacity('G'))
            if (nuke_.store.getUsedCapacity('energy') >= 300000)
            {
                /* 恢复terminal的原先数据 */
                if (thisTask.Data)
                for (var ii in thisTask.Data)
                {
                    this.memory.TaskData.TerminalResource[ii] = thisTask.Data[ii]
                }
                this.deleteMisson_ID(thisTask.ID)
            }
            else
            {
                if (nuke_.store.getUsedCapacity('energy') >= 300000)
                    this.deleteMisson_ID(thisTask.ID)
                if (storage_.store.getUsedCapacity('energy')+nuke_.store.getUsedCapacity('energy') < 300000)
                {
                    if (!thisTask.switch && Game.market.credits >= 150000)
                    {
                        Game.market.createOrder({
                            type: ORDER_BUY,
                            resourceType: 'energy',
                            price: 0.5,
                            totalAmount: 300000,
                            roomName: this.name  
                        });
                        thisTask.switch = true
                    }
                    return
                }
                if (this.CheckInMisson('CREEP','物流搬运')) return
                let Task2:RoomTask = {} as RoomTask
                Object.assign(Task2,MissonModel['物流搬运'])
                Task2.relateCreep = ['transport01']
                Task2.disnum = 300000
                Task2.relateResource = ['energy']
                Task2.disStructure = [nuke_.id]
                Task2.delayTick = 50
                Task2.sourceStructure = [this.memory.TaskMemory.storageID as string]
                this.mountMisson(Task2)
            }
        }
        
    }
    /* 控制冲级任务主动处理 */
    public CIL_AC():void{
        var thisTask = this.getMisson("CREEP","控制冲级")
        if (!thisTask) return
        var ULID = this.memory.TaskMemory.upgrade_link as string
        var CLID = this.memory.TaskMemory.center_link as string
        if (!ULID || !CLID) return
        var center_link =  Game.getObjectById(CLID as string) as StructureLink
        var upgrade_link = Game.getObjectById(ULID as string) as StructureLink
        if (!upgrade_link || !center_link) {delete this.memory.TaskMemory.upgrade_link;delete this.memory.TaskMemory.center_link;return}
        /* 爬虫数量控制 */
        global.DicCreepFrameControl[this.name]['i-upgrader'].num = thisTask.numData['i-upgrader']
        /* upgrade_link逻辑 在energy < 400 时，触发该逻辑 */
        if (upgrade_link.store['energy'] < 400)
        {
            if (center_link.store['energy'] >= 450)
            {
                if (this.CheckMissonNum('STRUCTURE',"链传送能") <= 3)
                {
                    var TaskStructure:RoomTask = {} as RoomTask
                    Object.assign(TaskStructure,MissonModel['链传送能'])
                    TaskStructure.disStructure = [upgrade_link.id]
                    TaskStructure.relateStructure = [center_link.id]
                    this.mountMisson(TaskStructure)
                }
            }
            else
            {
                var sw = false
                for (var i of this.memory.Task['CREEP'])
                {
                    if (i.Name == '物流搬运' && isInArray(i.relateCreep,'manage01'))
                        sw = true
                }
                if (this.CheckMissonNum('CREEP',"物流搬运") <= 2 && !sw)
                {
                    var TaskCreep:RoomTask = {} as RoomTask
                    Object.assign(TaskCreep,MissonModel['物流搬运'])
                    TaskCreep.relateCreep = ['manage01']
                    TaskCreep.disnum = 450
                    TaskCreep.relateResource = ['energy']
                    TaskCreep.disStructure = [center_link.id]
                    TaskCreep.taskLevel = 1
                    TaskCreep.sourceStructure = [this.memory.TaskMemory.storageID as string]
                    this.mountMisson(TaskCreep)
                }
            }
        }
        if (!thisTask.switch)
        {
            if (!thisTask.LabBind) return
            /* 有boost时的boost重启 */
            var storage_ = Game.getObjectById(this.memory.TaskMemory.storageID as string) as StructureStorage
            if (!storage_) return
            if (storage_.store[thisTask.relateResource[0]] > 5000)
            {
                thisTask.switch = true
            }
        }
        else
        {
            /* boost */
            for (var l in thisTask.LabBind)
            {
                var lLab = Game.getObjectById(thisTask.LabBind[l][0]) as StructureLab
                for (var r in lLab.store)
                {
                    if (r != 'energy' && r != thisTask.LabBind[l][1] && !this.getMisson_S('CREEP','物流搬运','transport01'))
                    {
                        /* 下搬运任务 */
                        var TaskCreep:RoomTask = {} as RoomTask
                        Object.assign(TaskCreep,MissonModel['物流搬运'])
                        TaskCreep.relateCreep = ['transport01']
                        TaskCreep.sourcenum = 0
                        TaskCreep.relateResource = [r as ResourceConstant]
                        TaskCreep.disStructure = [this.memory.TaskMemory.storageID as string]
                        TaskCreep.taskLevel = 1
                        TaskCreep.sourceStructure = [lLab.id]
                        this.mountMisson(TaskCreep)
                        return
                    }
                }
                /* 开始搬运 */
                if (lLab.store.getUsedCapacity(thisTask.LabBind[l][1]) < 1500 && !this.getMisson_S('CREEP','物流搬运','transport01'))
                {
                    var storage_ = Game.getObjectById(this.memory.TaskMemory.storageID as string) as StructureStorage
                    if (!storage_) return
                    if (lLab.store[thisTask.LabBind[l][1]] + storage_.store[thisTask.LabBind[l][1]] < 2400)
                    {
                        thisTask.switch = false
                    }
                    /* 下搬运任务 */
                    var TaskCreep:RoomTask = {} as RoomTask
                    Object.assign(TaskCreep,MissonModel['物流搬运'])
                    TaskCreep.relateCreep = ['transport01']
                    TaskCreep.disnum = 2000
                    TaskCreep.relateResource = [thisTask.LabBind[l][1]]
                    TaskCreep.disStructure = [lLab.id]
                    TaskCreep.taskLevel = 2
                    TaskCreep.sourceStructure = [this.memory.TaskMemory.storageID as string]
                    this.mountMisson(TaskCreep)
                    return
                }
            }
        }
    }
    /* 刷墙任务主动处理 */
    public CRW_AC():void{
        if (Game.time % 6) return
        if (this.CheckMissonNum('CREEP','爬虫刷墙') <= 0) return
        /* 数量管理 */
        var numData = {}
        for (var misson of this.memory.Task['CREEP'])
        {
            /* 数量控制 */
            if (misson.Name == '爬虫刷墙')
            {
            for (var c in misson.bindData)
            {
                if (!numData[c]) numData[c] = misson.bindData[c].num
                else
                {
                    numData[c] += misson.bindData[c].num
                }
            }
            }
        }
        /* 爬虫生产 */
        for (var i in numData)
        {
            global.DicCreepFrameControl[this.name][i].num = numData[i]
        }
    }
    /* 强化刷墙任务主动处理 */
    public BRW_AC():void{
        if (Game.time % 6) return
        if (this.CheckMissonNum('CREEP','强化刷墙') <= 0) return
        /* 数量管理 */
        var numData = {}
        
        for (var misson of this.memory.Task['CREEP'])
        {
            if (misson.Name == '强化刷墙')
            {
                for (var i in misson.LabBind)
                {
                    var drType = misson.LabBind[i][1]
                    var thisLab = Game.getObjectById(misson.LabBind[i][0]) as StructureLab
                    if (!thisLab) return
                    if (thisLab.store[drType] < 900 && !this.getMisson_S('CREEP','物流搬运','transport01'))
                    {
                        var TaskCreep:RoomTask = {} as RoomTask
                        Object.assign(TaskCreep,MissonModel['物流搬运'])
                        TaskCreep.relateCreep = ['transport01']
                        TaskCreep.disnum = 1200
                        TaskCreep.relateResource = [drType]
                        TaskCreep.disStructure = [thisLab.id]
                        TaskCreep.taskLevel = 1
                        TaskCreep.sourceStructure = [this.memory.TaskMemory.storageID as string]
                        this.mountMisson(TaskCreep)
                        return
                    }
                }
                for (var c in misson.bindData)
                {
                    if (!numData[c]) numData[c] = misson.bindData[c].num
                    else
                    {
                    numData[c] += misson.bindData[c].num
                    }
                }
            }
        }
        /* 爬虫生产 */
        for (var i in numData)
        {
            global.DicCreepFrameControl[this.name][i].num = numData[i]
        }
    }
    /* 扩张援建任务主动处理 */
    public EHB_AC():void{
        if (Game.time % 31) return
        if (this.CheckMissonNum('CREEP','扩张援建') <= 0) return
        var numData = {}
        for (var misson of this.memory.Task['CREEP'])
        {
            if (misson.Name == '扩张援建')
            {
                if (Game.rooms[misson.disRoom[0]] && Game.rooms[misson.disRoom[0]].controller && Game.rooms[misson.disRoom[0]].controller.my)
                {
                    misson.bindData['e-claimer'].num = 0
                }
                for (var c in misson.bindData)
                {
                    if (!numData[c]) numData[c] = misson.bindData[c].num
                    else
                    {
                        numData[c] += misson.bindData[c].num
                    }
                }
            }
        }
        /* 爬虫生产 */
        for (var i in numData)
        {
            global.DicCreepFrameControl[this.name][i].num = numData[i]
        }
    }
    /* 资源掠夺任务主动处理 */
    public RP_AC():void{
        if (Game.time % 17) return
        if (this.CheckMissonNum('CREEP','资源掠夺') <= 0) return
        var numData = {}
        for (var misson of this.memory.Task['CREEP'])
        {
            if (misson.Name == '资源掠夺')
            {
                if (misson.state == 1)
                {
                    for (var c in misson.bindData)
                    {
                    if (!numData[c]) numData[c] = misson.bindData[c].num
                    else
                    {
                        numData[c] += misson.bindData[c].num
                    }
                    }
                }
                else if (misson.state == 2)
                {
                    for (var c in misson.bindData)
                        misson.bindData[c].num = 0
                    var allrelateScreeps = _.filter(Game.creeps,(creep) => creep.memory.role == 'pillage' && creep.memory.belong == this.name && creep.memory.bindTask == misson.ID)
                    if (allrelateScreeps.length <= 0)
                        this.deleteMisson_ID(misson.ID)
                }
            }
        }
        /* 爬虫生产 */
        for (var i in numData)
        {
            global.DicCreepFrameControl[this.name][i].num = numData[i]
        }
    }
    /* power采集任务主动处理 */
    public PH_AC():void{
        var thisTask = this.getMisson('ROOM','power采集')
        if (!thisTask) return
        /* 查找pb阶段 */
        if (thisTask.state == 0)
        {
            if (Game.flags[`${this.name}/PB`]) Game.flags[`${this.name}/PB`].remove()
            var length = thisTask.disRoom.length
            if (length <=0) return
            if (!thisTask.index) thisTask.index = 0
            for (var i in thisTask.disRoom)
            {
                /* 访问指定房间 */
                LoopDis:
                if (thisTask.index == Number(i))
                {
                    var dRoom = thisTask.disRoom[i]
                    if (!Game.rooms[dRoom])
                    {
                        if (Game.time % 10) return
                        /* 无法访问就调用观察者进行观察 */
                        if (this.getMisson('STRUCTURE','观察房间')) return
                        if (!this.memory.TaskMemory.ObserverID) return
                        var Observer = Game.getObjectById(this.memory.TaskMemory.ObserverID as string) as StructureObserver
                        if (!Observer) {delete this.memory.TaskMemory.ObserverID;return}
                        var TaskStructure:RoomTask = {} as RoomTask
                        Object.assign(TaskStructure,MissonModel['观察房间'])
                        TaskStructure.relateStructure = [Observer.id]
                        TaskStructure.disRoom = [dRoom]
                        this.mountMisson(TaskStructure)
                        return
                    }
                    else
                    {
                        console.log("[PowerBank]正在观察房间",dRoom)
                        /* 寻找符合条件的PB */
                        var PB = Game.rooms[dRoom].find(FIND_STRUCTURES,{filter:(structure)=>{
                        return structure.structureType == STRUCTURE_POWER_BANK && structure.ticksToDecay >= 3800 && structure.pos.lookFor(LOOK_FLAGS).length <= 0 && structure.power > 3000
                        }})
                        if (PB.length > 0)
                        {
                        /* 下达采集任务 进入1状态 */
                        var TaskCreep:RoomTask = {} as RoomTask
                        Object.assign(TaskCreep,MissonModel['pb攻击'])
                        TaskCreep.disRoom = [dRoom]
                        TaskCreep.disStructure = [PB[0].id]
                        TaskCreep.relateCreep = ['power-attack','power-heal']
                        TaskCreep.bindData = {'power-attack':{num:2,bind:[]},'power-heal':{num:2,bind:[]}}
                        if(!this.mountMisson(TaskCreep)) return
                        PB[0].pos.createFlag(`${this.name}/PB`,COLOR_RED,COLOR_CYAN)
                        thisTask.state = 1
                        thisTask.HandleRoom = dRoom
                        return
                        }
                        /* 观察房间的索引 */
                        if (thisTask.index < length-1)
                        {
                            thisTask.index ++
                        }
                        else
                        {
                            thisTask.index = 0
                        }
                    }
                    break LoopDis

                }
            }
        }
        /* 双人小队攻击阶段 */
        else if (thisTask.state == 1)
        {
            global.DicCreepFrameControl[this.name]['power-attack'].num = 1
            global.DicCreepFrameControl[this.name]['power-heal'].num = 1
            var CreepTask = this.getMisson("CREEP",'pb攻击')
            if (!CreepTask) thisTask.state = 3
            for (var D in CreepTask.bindData)
                for (var c of CreepTask.bindData[D].bind)
                {
                    if (Game.creeps[c] && Game.creeps[c].ticksToLive < 350)
                    {
                        global.DicCreepFrameControl[this.name][D].num = 2
                    }
                }
            var PBAtask = this.getMisson("CREEP",'pb攻击')
            var PBflag = Game.flags[`${this.name}/PB`]
            if (!PBAtask || !PBflag) return
            var disRoom = thisTask.HandleRoom
            if (Game.rooms[disRoom])
            {
                /* 观察阶段 */
                var PBank = PBflag.pos.GetStructure(STRUCTURE_POWER_BANK)
                if (!PBank)
                {
                    /* 未发现就删除任务并回到状态0 */
                    Game.notify(`房间${this.name}的pb任务出现未知问题，任务状态调整为0! (暂未调整 排错中)`)
                    return
                    // this.deleteMisson('CREEP','pb攻击')
                    // thisTask.state = 0
                }
                if (PBank.hits < 200000)
                {
                    thisTask.state = 2
                }
            }
        }
        /* 搬运阶段 */
        else if (thisTask.state == 2)
        {
            var disRoom = thisTask.HandleRoom
            if (Game.rooms[disRoom])
            {
                var thisPB = Game.flags[`${this.name}/PB`].pos.GetStructure(`powerBank`) as StructurePowerBank
                if (!thisPB) {thisTask.state = 3}
                var powerNum = thisPB.power
                global.DicCreepFrameControl[this.name]['power-carry'].num = Math.ceil(powerNum/1250)
                if (!this.CheckInMisson('CREEP',"pb运输"))
                {
                    /* 下运输任务 */
                    var TaskCreep:RoomTask = {} as RoomTask
                    Object.assign(TaskCreep,MissonModel['pb运输'])
                    TaskCreep.disRoom = [disRoom]
                    TaskCreep.disStructure = [thisPB.id]
                    TaskCreep.relateCreep = ['power-carry']
                    this.mountMisson(TaskCreep)
                }
                
            }
        }
        /* 收尾阶段 */
        else if (thisTask.state == 3)
        {
            /* 寻找所有活着的运输爬虫 */
            if (Game.time % 10)
            {
                var carryCreep = _.filter(Game.creeps,(creep) => creep.memory.role == 'power-carry' && creep.memory.belong == this.name)
                if (carryCreep.length <= 0)
                    this.deleteMisson('CREEP','pb运输')
            }
            if (!this.getMisson('CREEP','pb运输') && !this.getMisson('CREEP','pb攻击'))
            {
                thisTask.state = 0
            }
        }
    }
    /* 搓Pixel任务主动处理 */
    public GP_AC():void{
        if (Game.time % 1400) return
        var thisTask = this.getMisson('ROOM','搓pixel')
        if (!thisTask) return
        global.DicCreepFrameControl[this.name]['pixel'].num = 1
    }
    
}